import { createRouter, createWebHistory, createWebHashHistory} from 'vue-router';
import pinia from '@/stores/index'
import { useCounterStore } from '../stores/counter'

import type { menu } from '../dataType/menu'
// import { qiankunWindow } from "vite-plugin-qiankun/dist/helper";
const store = useCounterStore(pinia)
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "home" */ '@/views/login.vue'),
    meta: {
      title: '登陆',
    },
  },
  {
    path: '/',
    name: 'Layout',
    redirect: '/home',
    component: () => import(/* webpackChunkName: "home" */ '@/Layout/index.vue'),
    meta: {
      title: '根路由',
    },
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import(/* webpackChunkName: "home" */ '@/views/index.vue'),
        meta: {
          title: '首页',
        },
      },
      {
        path: '/course',
        name: 'course',
        component: () => import(/* webpackChunkName: "home" */ '../views/course/course.vue'),
        meta: {
          title: '课程',
        },
      },
      {
        path: '/chapter',
        name: 'chapter',
        component: () => import(/* webpackChunkName: "home" */ '../views/course/chapter.vue'),
        meta: {
          title: '章节',
        },
      },
      {
        path: '/category',
        name: 'category',
        component: () => import(/* webpackChunkName: "home" */ '../views/course/category.vue'),
        meta: {
          title: '课程',
        },
      },
      {
        path: '/homework',
        name: 'homework',
        component: () => import(/* webpackChunkName: "home" */ '../views/course/homework.vue'),
        meta: {
          title: '布置作业',
        },
      },
      {
        path: '/assignHomework',
        name: 'assignHomework',
        component: () => import(/* webpackChunkName: "home" */ '../views/course/assignHomework.vue'),
        meta: {
          title: '发布作业',
        },
      },
      {
        path: '/updateInfo',
        name: 'updateInfo',
        component: () => import(/* webpackChunkName: "home" */ '../views/setUp/updateInfo.vue'),
        meta: {
          title: '修改资料',
        },
      },
      {
        path: '/updatePassword',
        name: 'updatePassword',
        component: () => import(/* webpackChunkName: "home" */ '../views/setUp/updataPassword.vue'),
        meta: {
          title: '更改密码',
        },
      },
      {
        path: '/homeworks',
        name: 'homeworks',
        component: () => import(/* webpackChunkName: "home" */ '../views/homework/index.vue'),
        meta: {
          title: '作业',
        },
      },
      {
        path: '/homeworkSubmit',
        name: 'homeworkSubmit',
        component: () => import(/* webpackChunkName: "home" */ '../views/homework/homeworkSubmit.vue'),
        meta: {
          title: '作业提交列表',
        },
      },
      
      {
        path: '/class',
        name: 'class',
        component: () => import(/* webpackChunkName: "home" */ '../views/class/index.vue'),
        meta: {
          title: '班级',
        },
      },
      {
        path: '/classesMember',
        name: 'classesMember',
        component: () => import(/* webpackChunkName: "home" */ '../views/class/classesMember.vue'),
        meta: {
          title: '班级成员',
        },
      },
      {
        path: '/classRecord',
        name: 'classRecord',
        component: () => import(/* webpackChunkName: "home" */ '../views/class/classRecord.vue'),
        meta: {
          title: '上课记录',
        },
      },
      {
        path: '/classesDetails',
        name: 'classesDetails',
        component: () => import(/* webpackChunkName: "home" */ '../views/class/classesDetails.vue'),
        meta: {
          title: '学员详情',
        },
      },
      {
        path: '/plugin',
        name: 'plugin',
        component: () => import(/* webpackChunkName: "home" */ '../views/plugin/index.vue'),
        meta: {
          title: '工具',
        },
      },
      {
        path: '/openClass',
        name: 'openClass',
        component: () => import(/* webpackChunkName: "home" */ '../views/openClass/index.vue'),
        meta: {
          title: '公开课',
        },
      },
      {
        path: '/addOpenClass',
        name: 'addOpenClass',
        component: () => import(/* webpackChunkName: "home" */ '../views/openClass/addOpenClass.vue'),
        meta: {
          title: '创建公开课',
        },
      },
    ],
  },
  {
    path: '/live',
    name: 'live',
    component: () => import(/* webpackChunkName: "home" */ '@/views/live.vue'),
    meta: {
      title: '直播',
    },
  },
  {
    path: '/createLive',
    name: 'createLive',
    component: () => import(/* webpackChunkName: "home" */ '@/views/createLive.vue'),
    meta: {
      title: '创建直播',
    },
  },
  {
    path: '/pseudoLive',
    name: 'pseudoLive',
    component: () => import(/* webpackChunkName: "home" */ '../views/openClass/pseudoLive.vue'),
    meta: {
      title: '伪直播',
    },
  },
];
// export const history = createWebHistory(
//   qiankunWindow.__POWERED_BY_QIANKUN__ ? '/main/first' : "/"
// );

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
});
router.beforeEach(async function (to, from, next) {
  // 路由前
  const token = localStorage.getItem('token') as never
  // 设置当前打开路由tab
  const inPath: menu = {
      title: to.meta.title as never,
      name: to.name as never, // 模版名称
      path: to.path, // 路由地址
  }
  if(token) {
    if (to.path == '/login') {
        next('/home')
        return
    }
  } else {
    if (to.path != '/login') {
        next('/login')
        return
    }
  }

  // 如果是第一次进来设置初始化标签
  // store.setCurrentRoute({ path: path, name: to.name as never })
  if (inPath.title) {
      store.setSelectCrumbs(inPath)
  }
  next()
})
export default router;
